package com.lanluo.service.structure;

/**
 * 符号表(基于无序链表 顺序查找)
 * @author
 * @date 2018-08-28-18:14
 */
public class SequentialSerchSt<Key, Value> {
    private Node first; //链表首节点

    /**
     * 链表节点定义
     */
    private class Node {
        Key key;
        Value val;
        Node next;

        public Node(Key key, Value val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }

    public Value get(Key key) {
        for(Node x = first; x!=null; x=x.next) {
            if(x.key.equals(key)) {
                return x.val;
            }
        }
        return null;
    }

    public void put(Key key, Value val) {
        for(Node x = first; x!=null; x=x.next) {
            if(x.key.equals(key)) {
                x.val=val;
                return;
            }
        }

        first = new Node(key, val, first);

    }

}